﻿using System;
using System.Data;
using System.Collections.Generic;
using SERP.DAO;
namespace SERP.Controllers{
public class RolesController : RolesControllerBase{
	public RolesController()
		: base()
	{
	}	
	private DataTable _Table;
	private RolesEntity oRoles;	
	public RolesEntity RolesObject{
		get{
			return oRoles;
		}
		set{
			oRoles=value;
		}
	}
	public DataTable Table{
		get{
			if(_Table==null&&AllowCaching){				
				_Table=Cache.Get<DataTable>(CACHE_KEY_DS);
			}
			return _Table;
		}
		set{
			_Table=value;
		}
	}
	public void LoadAllDSRoles(bool clearCache){
		bool bForce=clearCache;
		if(bForce||_Table==null)
		{
			DataSet ds=Service.SelectAllDS();
			_Table=ds.Tables[0];
			AddIndexToTable(_Table);
			if(AllowCaching)
				AddCache(CACHE_KEY_DS,_Table);			
		}				
	}	
	public void AddIndexToTable(){
		AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		ResetTableIndex(_Table);
	}
	public void LoadObject(Int32 RoleId){
		RolesEntity oLoad=FindRoles(RoleId);
		if(oLoad==null){
			LoadAllRoles(true);
			oRoles=FindRoles(RoleId);
		}
		else{
			oRoles=oLoad;
		}
	}
	public bool AddNew(){
		if(oRoles==null)return false;
		bool result = Service.Save(oRoles);
		if(result){
			AddObjectToCache(oRoles);
		}
		return result;
	}
	public bool Update(){
		if(oRoles==null)return false;
		return Service.Update(oRoles);	
	}
	public bool Delete(){
		if(oRoles==null)return false;		
		bool retvalue = Service.Delete(oRoles);
		if(retvalue){			
			RemoveObjectFromCache(oRoles);
		}
		return retvalue;
	}	
	public bool DeleteByID(Int32 RoleId){
		bool retvalue = Service.DeleteByID(RoleId);		
		if(retvalue){
			RolesEntity oDeleted=FindRoles(RoleId);
			RemoveObjectFromCache(oDeleted);
		}
		return retvalue;
	}	
	public void ResetObject(){
		this.oRoles = new RolesEntity();
	}
	public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.RolesObject,row);
    }
    public RolesEntity LoadRoles(Int32 RoleId){			
		RolesEntity o=Service.SelectOne(RoleId);		
		return o;
	}
	#region Cached
    private static List<RolesEntity>_ListRoles;
	private static bool _AllowCaching=true;
	public static bool AllowCaching{
		get{return _AllowCaching;}
		set{AllowCaching=value;}
	}	
	public static void ClearCache(){
			if(!_AllowCaching)return;
			Cache.Remove(CACHE_KEY_DS);
			Cache.Remove(CACHE_KEY_LS);
	}
	private const string CACHE_KEY_DS="RolesDS";
	private const string CACHE_KEY_LS="RolesLS";
	private void AddObjectToCache(RolesEntity newObject){
		if(!AllowCaching)return;
		GetCacheList().Add(newObject);
	}
	private void RemoveObjectFromCache(RolesEntity oRemove)
	{
		if(!AllowCaching)return;
		if(oRemove!=null)
		GetCacheList().Remove(oRemove);
	}
	private static List<RolesEntity>GetCacheList(){
		if(Cache==null)return default(List<RolesEntity>);
		List<RolesEntity>ls= Cache.Get<List<RolesEntity>>(CACHE_KEY_LS);
		if(ls==null){
			ls = new List<RolesEntity>();
			AddCache(CACHE_KEY_LS,ls);
		}
		return ls;
	}	
	public static Dictionary<Int32,RolesEntity> GetRolesDictionary(){
		Dictionary<Int32,RolesEntity> dic=new Dictionary<Int32,RolesEntity>();
		foreach(RolesEntity o in ListRolesStatic){
			dic.Add(o.RoleId,o);
		}
		return dic;
	}
	public List<RolesEntity>ListRoles{
		get{
			return LoadAllRoles(false);
		}
	}
	public static List<RolesEntity>ListRolesStatic{
		get{
			LoadAllRoles(false);
			return _ListRoles;
		}
	}
	public RolesEntity FindRoles(Int32 RoleId){
		if(AllowCaching){
			_ListRoles=GetCacheList();
		}		
		RolesEntity oResult=_ListRoles.Find(delegate(RolesEntity oFind){
			bool result=true; 
			result&=oFind.RoleId.Equals(RoleId);
			return result;
		 });
		return oResult;
	}
	public static List<RolesEntity>LoadAllRoles(bool clearCache){
		_ListRoles = new List<RolesEntity>();
		if(AllowCaching){
			_ListRoles=GetCacheList();
		}
		if(_ListRoles==null||_ListRoles.Count==0)
			clearCache=true;
		if(!clearCache)return _ListRoles;
		_ListRoles=new List<RolesEntity>(Service.SelectAll());		
		if(AllowCaching)
			AddCache(CACHE_KEY_LS,_ListRoles);
		return _ListRoles;
	}
	#endregion
}
}
